<template>
  <div class="index-container">
    <!-- <van-nav-bar title="单选题练习" left-text="返回" left-arrow @click-left="onClickLeft" @click-right="onClickRight" /> -->
    <div class="text">
      <div style="display: flex; justify-content: space-between; align-items: center;">
      <span class="bt">单选题练习 {{ index + 1 }}</span>
      <div class="loader"></div>
      <div class="goback" @click="onClickLeft">返回首页</div>
     </div>
      <p>{{ getDate?.title }}</p>
      <div class="btn">
        <van-radio-group v-model="radio">
          <van-radio
            :name="item.name"
            checked-color="#ee0a24"
            style="margin-top: 10px"
            v-for="(item, index) in getDate?.data"
            :key="index"
          >
            <div style="color: #fff">{{ item.lable }}</div>
          </van-radio>
        </van-radio-group>
      </div>
      <div class="checked">
        <van-button round type="warning" block @click="handleCheck">确定</van-button>
      </div>
    </div>
  </div>
</template>

<script>
import { getMiddleware_s } from '../../api/home.js'
import { Dialog } from 'vant'
export default {
  name: 'Radio',
  data() {
    return {
      radio: '',
      list: [],
      index: 0,
      noNnswer: []
    }
  },
  computed: {
    getDate() {
      return this.list[this.index]
    },
    getText() {
      return 100 - (this.noNnswer.length / this.list.length) * 100 + '%'
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      // let params = {
      //   size: 5
      // }
      getMiddleware_s().then(result => {
        if (result.code == 200) {
          this.list = result.data.map(item => {
            return {
              title: item.title,
              answer: item.answer,
              data: ['A', 'B', 'C', 'D'].map(val => {
                return {
                  name: val,
                  lable: val +'、'+item[val]
                }
              })
            }
          })
        }
      })
    },
    handleCheck() {
      if( this.radio == ''){
        Dialog.alert({
            message: '请选择答案',
            theme: 'round-button'
          })
        return
      }
      if (this.radio == this.getDate.answer) {
        if (this.index < this.list.length - 1) {
          this.index++
          this.radio = ''
        } else {
          Dialog.alert({
            message: `恭喜您完成练习${this.list.length}道题,</br>答错${this.noNnswer.length}道题，答题成功率为${this.getText}</br> 祝您考试顺利！`,

            theme: 'round-button'
          }).then(() => {
            this.$router.push({
              name: 'Home'
            })
          })
        }
      } else {
        Dialog.alert({
          message: `答案错误正确答案是${this.getDate.answer}`,
          theme: 'round-button'
        }).then(() => {
          this.noNnswer = [...new Set([...this.noNnswer, this.index])]
        })
      }
    },
    onClickLeft() {
      this.$router.go(-1)
    },
    onClickRight() {}
  }
}
</script>

<style scoped lang="less">
.index-container {
  width: 100%;
  height: 100vh;
  background: url('../../assets/imgs/4546.png') no-repeat 100% 100%;
  background: #4e6ef2;
  color: #fff;
  overflow: hidden;
  box-sizing: border-box;
  .goback{
    width: 90px;
    height: 25px;
    border: 1px solid #fff;
    border-radius: 15px;
    text-align: center;
    line-height: 25px;
  }
 /* HTML: <div class="loader"></div> */
.loader {
  --c1:#673b14;
  --c2:#f8b13b;
  width: 30px;
  height: 40px;
  border-top: 4px solid var(--c1);
  border-bottom: 4px solid var(--c1);
  background: linear-gradient(90deg, var(--c1) 2px, var(--c2) 0 5px,var(--c1) 0) 50%/7px 8px no-repeat;
  display: grid;
  overflow: hidden;
  animation: l5-0 5s infinite linear;
}
.loader::before,
.loader::after {
  content: "";
  grid-area: 1/1;
  width: 75%;
  height: calc(50% - 4px);
  margin: 0 auto;
  border: 2px solid var(--c1);
  border-top: 0;
  box-sizing: content-box;
  border-radius: 0 0 40% 40%;
  -webkit-mask: 
    linear-gradient(#000 0 0) bottom/4px 2px no-repeat,
    linear-gradient(#000 0 0);
  -webkit-mask-composite: destination-out;
          mask-composite: exclude;
  background: 
    linear-gradient(var(--d,0deg),var(--c2) 50%,#0000 0) bottom /100% 205%,
    linear-gradient(var(--c2) 0 0) center/0 100%;
  background-repeat: no-repeat;
  animation: inherit;
  animation-name: l5-1;
}
.loader::after {
  transform-origin: 50% calc(100% + 2px);
  transform: scaleY(-1);
  --s:3px;
  --d:180deg;
}
@keyframes l5-0 {
  80%  {transform: rotate(0)}
  100% {transform: rotate(0.5turn)}
}
@keyframes l5-1 {
  10%,70%  {background-size:100% 205%,var(--s,0) 100%}
  70%,100% {background-position: top,center}
}
  .bt{
    line-height: 25px;
  }
  .text {
    padding: 20px;
  }
  .btn {
    padding: 20px;
    width: 100%;
    height: 200px;
    margin: 0 auto;
  }
  .checked {
    width: 250px;
    height: 50px;
    margin: 0 auto;
    position: fixed;
    left: 62.5px;
    bottom: 100px;
  }
}
</style>
